package com.example.tabbar.fragment


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.adapter.NavigationAdapter
import com.example.tabbar.room.bean.NavItem
import com.example.tabbar.databinding.FragmentSetBinding


class SetFragment : Fragment() {
    private var binding: FragmentSetBinding? = null
    private var accountFragment: AccountFragment? = null
    private var recyclerView: RecyclerView? = null
    private var adapter: NavigationAdapter? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        binding = FragmentSetBinding.inflate(inflater, container, false)
        return binding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (savedInstanceState == null) {
            accountFragment = AccountFragment.newInstance()
            loadFragment(accountFragment!!)
        }

        initRecyclerView()
    }

    private fun initRecyclerView() {
        recyclerView = binding?.recyclerView
        adapter = NavigationAdapter()
        recyclerView?.adapter = adapter
        recyclerView?.layoutManager = LinearLayoutManager(requireContext())

        val navItems = navItems()
        adapter?.setItems(navItems)
        val setTimeFragment = SetTimeFragment.newInstance()
        val setShowFragment = SetShowFragment.newInstance()
        val setParameterFragment = SetParameterFragment.newInstance()
        val setFileFragment = SetFileFragment.newInstance()
        val setPrintFragment = SetPrintFragment.newInstance()
        val appMsgFragment = AppMsgFragment.newInstance()

        // 设置点击监听
        adapter?.setOnItemClickListener { position ->
            // 处理选中项的逻辑
            when (position) {
                0 -> {
                    // 处理首页点击
                    loadFragment(accountFragment!!)
                }

                1 -> {
                    loadFragment(setTimeFragment)
                }

                2 -> {
                    loadFragment(setShowFragment)
                }

                3 -> {
                    //参数设置
                    loadFragment(setParameterFragment)
                }

                4 -> {
                    // 处理文件管理点击
                    loadFragment(setFileFragment)
                }

                5 -> {
                    // 处理打印设置点击
                    loadFragment(setPrintFragment)
                }

                6 -> {
                    // 处理仪器信息点击
                    loadFragment(appMsgFragment)
                }
            }
        }
    }

    private fun navItems(): List<NavItem> {
        val navItems = listOf(
            NavItem(R.drawable.users, getString(R.string.tb_setting_account)),
            NavItem(R.drawable.time, getString(R.string.tb_setting_time)),
            NavItem(R.drawable.show, getString(R.string.tb_setting_show)),
            NavItem(R.drawable.parameter, getString(R.string.tb_setting_parameter)),
            NavItem(R.drawable.file_manager, getString(R.string.tb_setting_file)),
            NavItem(R.drawable.print, getString(R.string.tb_setting_print)),
            NavItem(R.drawable.instrument, getString(R.string.tb_setting_measurement_about))
        )
        return navItems
    }

    private fun loadFragment(fragment: Fragment) {

        // 使用hide/show代替replace提高性能
        val transaction = childFragmentManager.beginTransaction()

        // 隐藏所有Fragment
        childFragmentManager.fragments.forEach {
            transaction.hide(it)
        }

        // 显示目标Fragment或添加新Fragment
        if (fragment.isAdded) {
            transaction.show(fragment)
        } else {
            transaction.add(R.id.set_content, fragment)
        }

        transaction.commit()
    }



    companion object {

        @JvmStatic
        fun newInstance() =
            SetFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}